package Databases;

import org.xml.sax.helpers.DefaultHandler;
import UserUnit.ImpPhoto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class PhotoHandler extends org.xml.sax.helpers.DefaultHandler {

    private ImpPhoto photo;
    private StringBuffer accumulator;
    String id_file, name, extension, tax_type, tax_type_name;
    int day, month, year, hour, minute, width, high;
    double size;
    HashMap<String, String> taxs = new HashMap<String, String>();
    List tags = new ArrayList<String>();

    private enum photoOption {

        id_file, name, extension, comment, url, day, month, year, hour, minute, width, high,
        size, tag, tax, file, upload_date, time, dimensions, tagging, tax_type, T, file_storage_PHOTO,
        tax_name, id_user, item;
    }

    public PhotoHandler() {
        photo = new ImpPhoto();
    }

    public PhotoHandler(ImpPhoto ph) {
        photo = ph;
    }

    // Called at the beginning
    public void startDocument() {
        accumulator = new StringBuffer();
    }

    // When the parser encounters plain text (not XML elements), it calls this method, which accumulates 
    // the text in a string buffer. Note that this method may be called in multiple times.
    public void characters(char[] buffer, int start, int length) {
        accumulator.append(buffer, start, length);
    }

    // At the beginning of each new element, erase any accumulated text
    public void startElement(String namespaceURL, String localName, String qname, Attributes attributes) {
        accumulator.setLength(0);

    }

    // Take special action when reach the end of selected elements.. 
    // In this case the accumulated text is stored in the photo object
    public void endElement(String namespaceURL, String localName, String qname) {
        // System.out.println("temporal tag: " + localName);

        switch (photoOption.valueOf(localName)) {

            case id_user:
                photo.setUserId(accumulator.toString().trim());
                break;
            case id_file:
                id_file = accumulator.toString().trim();
                photo.setFile(id_file, name, extension);
                break;
            case name:
                name = accumulator.toString().trim();
                photo.setFile(id_file, name, extension);
                break;
            case extension:
                extension = accumulator.toString().trim();
                photo.setFile(id_file, name, extension);
                break;
            case comment:
                photo.setComment(accumulator.toString().trim());
                break;
            case url:
                photo.setUrl(accumulator.toString().trim());
                break;
            case day:
                day = Integer.parseInt(accumulator.toString().trim());
                photo.setUpdateDate(day, month, year);
                break;
            case month:
                month = Integer.parseInt(accumulator.toString().trim());
                photo.setUpdateDate(day, month, year);
                break;
            case year:
                year = Integer.parseInt(accumulator.toString().trim());
                photo.setUpdateDate(day, month, year);
                break;
            case hour:
                hour = Integer.parseInt(accumulator.toString().trim());
                photo.setHour(hour, minute);
                break;
            case minute:
                minute = Integer.parseInt(accumulator.toString().trim());
                photo.setHour(hour, minute);
                break;
            case width:
                width = Integer.parseInt(accumulator.toString().trim());
                photo.setDimension(width, high);
                break;
            case high:
                high = Integer.parseInt(accumulator.toString().trim());
                photo.setDimension(width, high);
                break;
            case size:
                size = Double.parseDouble(accumulator.toString().trim().replaceAll("[,]", "."));
                photo.setSize(size);
                break;
            case tag:
                tags.add(accumulator.toString().trim());
                photo.setTags(tags);
                break;
            case tax_type:
                tax_type = accumulator.toString().trim();
                break;
            case tax_name:
                tax_type_name = accumulator.toString().trim();
                taxs.put(tax_type, tax_type_name);
                photo.setTaxs(taxs);

            default:
                break;
        }


    }

    // Called at the end
    public void endDocument() {
    }

    public void warning(SAXParseException exception) {
        System.err.println("WARNING: line " + exception.getLineNumber() + ": "
                + exception.getMessage());
    }

    public void error(SAXParseException exception) {
        System.err.println("ERROR: line " + exception.getLineNumber() + ": "
                + exception.getMessage());
    }

    public void fatalError(SAXParseException exception) throws SAXException {
        System.err.println("FATAL: line " + exception.getLineNumber() + ": "
                + exception.getMessage());
        throw (exception);
    }
}
